<?
// IRIS: I Read It Somewhere
// http://ireaditsomewhere.googlecode.com 

$IRIS_VERSION = '1.3';

ini_set('display_errors', 'On');
session_start();
require('config.php');
require('libs/Snoopy.class.php');
require('libs/refreshPage.php');
require('libs/Smarty/Smarty.class.php');
require('libs/class.db.php');
require('libs/class.bibtex.php');
require('libs/class.user.php');

$smarty = new Smarty();
$dbObj = new DB();
$db = $dbObj->getDB();
$bibtex = new Bibtex();
$user = new User($db);
$error = array();
$note = array();
$smarty->plugins_dir[] = 'libs/CustomSmartyPlugins';
$rootdir = substr($_SERVER["SCRIPT_FILENAME"], 0, strlen($_SERVER["SCRIPT_FILENAME"]) - 10).'/';

if (!isset($tmpdir) || (isset($tmpdir) && $tmpdir == '')) {
    $tmpdir = sys_get_temp_dir();
}

if (!is_writable($smarty->compile_dir)) {
    die('Sorry, but <code>templates_c</code> is not writable by the webserver.  This is required for IRIS to work correctly.  Please modify the permissions of this folder to ensure the webserver can write to it and then refresh this page.');
}

$smarty->assign('IRIS_VERSION', $IRIS_VERSION);
$smarty->assign('IRIS_URL', $IRIS_URL);

// Figure out the page we want to display
$page = 'summary';
if (isset($_GET['p']))
{
	$page = $_GET['p'];
}
if (isset($_POST['p']))
{
	$page = $_POST['p'];
}
$smarty->assign('page', $page);

// Run the basic security code to prevent unauthorized access.
$smarty->assign('user_loggedIn', $user->loggedIn());
$smarty->assign('user_username', $user->getUsername());
$smarty->assign('user_admin', $user->isAdmin());
$loginReq = array('add', 'edit', 'config', 'account', 'logout', 'metafields');
$adminReq = array('config');
if (!$user->loggedIn() && in_array($page, $loginReq))
{
    $page = 'summary';
}
if (!$user->isAdmin() && in_array($page, $adminReq))
{
    $page = 'summary';
}
if (isset($_GET['login']))
{
    $note[] = 'Login Successful!';
}
if (isset($_GET['logout']))
{
    $note[] = 'Logout Successful!';
}
$admins = $user->getAdministrators();
$admins_text = '';
foreach($admins as $a)
{
    $admins_text .= '<strong>'.$a['firstname'].' '.$a['lastname'].'<br />'.$a['email'].'<br /><br />';
}
$smarty->assign('administrators', $admins_text);

// Display the page we want to display
switch($page)
{
    case 'bibtex':
        require('pages/bibtex.php');
        $smarty->display('bibtex.tpl');
        return;
    break;

	case 'projects':
		require('pages/projects.php');
		$smarty->assign('template', 'projects');
		$smarty->assign('page_name', 'Projects');
	break;

	case 'add':
		require('pages/add.php');
		$smarty->assign('template', 'add');
		$smarty->assign('page_name', 'Add Reference');
	break;

	case 'edit':
		require('pages/add.php');
		$smarty->assign('template', 'add');
		$smarty->assign('page_name', 'Edit Reference');
	break;

	case 'view':
		require('pages/view.php');
		$smarty->assign('template', 'view');
		$smarty->assign('page_name', 'View Reference');
	break;

	case 'delete':
		require('pages/view.php');
		require('pages/delete.php');
		$smarty->assign('template', 'view');
		$smarty->assign('page_name', 'Delete Reference');
	break;

	case 'login':
		require('pages/login.php');
		$smarty->assign('template', 'login');
		$smarty->assign('page_name', 'User Login');
	break;

	case 'tools':
		$smarty->assign('template', 'tools');
		$smarty->assign('page_name', 'Download Tools');
	break;

	case 'search':
		require('pages/search.php');
		$smarty->assign('template', 'search');
		$smarty->assign('page_name', 'Search');
	break;

	case 'feed':
		require('pages/feed.php');
		$smarty->display('feed.tpl');
        die();
	break;

	case 'config':
		require('pages/config.php');
		$smarty->assign('template', 'config');
		$smarty->assign('page_name', 'Configuration');
	break;

	case 'logout':
		require('pages/logout.php');
	break;

	case 'metafields':
		require('pages/metafields.php');
		$smarty->assign('template', 'metafields');
		$smarty->assign('page_name', 'Meta Fields');
	break;

	case 'account':
		require('pages/account.php');
		$smarty->assign('template', 'account');
		$smarty->assign('page_name', 'Account Options');
	break;

	case 'summary':
	default:
		require('pages/summary.php');
		$smarty->assign('template', 'summary');
		$smarty->assign('page_name', 'Summary');
	break;
}

$smarty->assign('error', $error);
$smarty->assign('note', $note);

$smarty->display('base.tpl');
?>
